package com.dji.sample.media.controller;

import com.dji.sample.media.model.AiResultVO;
import com.dji.sample.media.model.MediaFileDTO;
import com.dji.sample.media.model.OutsideRequestEntity;
import com.dji.sample.media.service.IFileService;
import com.dji.sample.media.service.IMediaService;
import com.dji.sample.wayline.service.IOssFileService;
import com.dji.sdk.common.HttpResultResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.time.ZoneId;

@Slf4j
@RestController
@RequestMapping("/outside/ai-detect")
public class OutSideAiDetectController {


    @Autowired
    RestTemplate restTemplate;

    @Autowired
    private IMediaService mediaService;

    @Autowired
    private IFileService fileService;

    @Autowired
    private IOssFileService  ossFileService;

    /**
     * 远程调用JAVA 2 Python AI接口进行图像检测
     * @return AiResultVO 检测结果
     */
    @PostMapping("/getDetect")
    public HttpResultResponse<AiResultVO> callPythonAiInterface(@RequestBody OutsideRequestEntity requestEntity) {

        // 添加调试日志
        log.info("Received request entity: {}", requestEntity);
        log.info("Request entity details - sourceFilePicId: {}, sourceFileUrl: {}, shootTime: {}, uploadTime: {}", 
                requestEntity.getSourceFilePicId(), 
                requestEntity.getSourceFileUrl(), 
                requestEntity.getShootTime(), 
                requestEntity.getUploadTime());

        //调用外部ai服务
        try {
            String url = "http://localhost:9022/api/ai/detect";

            // 设置请求头为 JSON
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);

            // 将请求实体封装进 HttpEntity 并带上 header
            HttpEntity<OutsideRequestEntity> entity = new HttpEntity<>(requestEntity, headers);

            // 添加调试日志 - 打印即将发送的请求
            log.info("Sending request to URL: {}", url);
            log.info("Request headers: {}", headers);
            log.info("Request body: {}", requestEntity);

            AiResultVO result = restTemplate.postForObject(url, entity, AiResultVO.class);
            
            log.info("Received response: {}", result);
            return HttpResultResponse.success(result);
        } catch (Exception e) {
            log.error("Failed to call AI service: ", e);
            return HttpResultResponse.error("Failed to call AI service");
        }
    }

//    /**
//     * 远程调用JAVA 2 Python AI接口进行图像检测
//     * @return AiResultVO 检测结果
//     */
//    @GetMapping("/getDetect/{workSpaceId}/{sourceFilePicId}")
//    public HttpResultResponse<AiResultVO> callPythonAiInterfaceTest(@PathVariable String workSpaceId,@PathVariable String sourceFilePicId) {
//        MediaFileDTO file = fileService.getFileByFileId(workSpaceId, sourceFilePicId);
//        String objectKey = file.getObjectKey();
//        String fileUrlByObjectKey = ossFileService.getFileUrlByObjectKey(objectKey);
//        //调用外部ai服务
//        try {
//            String url = "http://localhost:9022/api/ai/detect/detail";
//            OutsideRequestEntity requestEntity = new OutsideRequestEntity();
//            requestEntity.setSourceFilePicId(sourceFilePicId);
//            requestEntity.setSourceFileUrl(fileUrlByObjectKey);
//            requestEntity.setShootTime(file.getCreateTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
//            requestEntity.setUploadTime(file.getCreateTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
//            AiResultVO result = restTemplate.postForObject(url, requestEntity, AiResultVO.class);
//            return HttpResultResponse.success(result);
//        } catch (Exception e) {
//            log.error("Failed to call AI service: ", e);
//            return HttpResultResponse.error("Failed to call AI service");
//        }
//    }
}
